/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.importdiamond;

import com.kmcsoft.goldshop.generic.service.ShopGenericService;
import com.kmcsoft.goldshop.importdiamond.service.ImportDiamondService;
import com.kmcsoft.goldshop.model.master.ProductDiamond;
import com.kmcsoft.goldshop.model.scm.ImportJewelryDetail;
import com.kmcsoft.goldshop.model.scm.ImportJewerlyOrder;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;

/**
 *
 * @author huyvu
 */
public class OrderInformation extends GenericForwardComposer {

    @Autowired
    private ImportDiamondService importDiamondService;
    @Autowired
    private ShopGenericService shopGenericService;
    private double totalWeight;
    private double totalCost;
    private double totalCostInBasicCurrency;
    private String code;
    private double totalAmount;
    private NumberFormat numberFormat;
    private List<HashMap<Object, String>> amounts;
    private AnnotateDataBinder binder;

    public List<HashMap<Object, String>> getAmounts() {
        return amounts;
    }

    public String getCode() {
        return code;
    }

    public String getTotalAmount() {
        return numberFormat.format(totalAmount);
    }

    public String getTotalCost() {
        return numberFormat.format(totalCost);
    }

    public String getTotalCostInBasicCurrency() {
       return numberFormat.format(totalCostInBasicCurrency);
    }

    

    public String getTotalWeight() {
        return numberFormat.format(totalWeight);
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        numberFormat = NumberFormat.getInstance();
        self.setAttribute("modal", this);
        ImportJewerlyOrder order = shopGenericService.getImportJewerlyOrderById_DetachDetail(((Long) arg.get("importOrderId")));
        code = "" + order.getCode();
        totalAmount = order.getDetails().size();
        totalWeight = 0;
        totalCost = 0;
        totalCostInBasicCurrency = 0;
        amounts = new ArrayList<HashMap<Object, String>>();
        for (ImportJewelryDetail detail : order.getDetails()) {
            ProductDiamond diamond = importDiamondService.getProductDiamondById(detail.getProduct().getId());
            totalWeight += diamond.getDiamondWeight();
            totalCost += diamond.getCost();
            totalCostInBasicCurrency += diamond.getCostInBasicCurrency();
            HashMap<Object, String> map = new HashMap<Object, String>();
            boolean flag = true;
            for (int i = 0; i < amounts.size(); i++) {
                HashMap<Object, String> hashMap = amounts.get(i);
                if (hashMap.get("type").equals(diamond.getDiamondGroup().getName()+":")) {
                    hashMap.put("amount", "" + (Integer.parseInt(hashMap.get("amount")) + 1));
                    flag = false;
                    break;
                }
            }
            if (flag) {
                map.put("type", diamond.getDiamondGroup().getName()+":");
                map.put("amount", "1");
                amounts.add(map);
            }

        }
        binder = new AnnotateDataBinder(self);
        self.setAttribute("binder", binder);
        binder.loadAll();
    }

    public void onDetach() {
        self.detach();
    }
}
